package toy.keli.picmap.model;

import android.util.Log;

/**
 * @see2 https://www.jianshu.com/p/582c3dac7d32
 */
public class log {

    public static void Info(String content, Object... args) {
        for (int i = 0; i < Thread.currentThread().getStackTrace().length; i++) {
            String realContent = getContent(content, i, args);
            Log.d("default", realContent);
        }
    }

    public static void PrintD(String content,String tag,  Object... args) {
        Log.d(tag, getContent(content, 4, args));
    }

    private static String getNameFromTrace(StackTraceElement[] traceElements, int place) {
        StringBuilder taskName = new StringBuilder();
        if (traceElements != null && traceElements.length > place) {
            StackTraceElement traceElement = traceElements[place];
            taskName.append(traceElement.getMethodName());
            taskName.append("(").append(traceElement.getFileName()).append(":").append(traceElement.getLineNumber()).append(")");
        }
        return taskName.toString();
    }

    private static String getContent(String msg, int place, Object... args) {
        try {
            String sourceLinks = getNameFromTrace(Thread.currentThread().getStackTrace(), place);
            return sourceLinks + String.format(msg, args);
        } catch (Throwable throwable) {
            return msg;
        }
    }

    public static void Waring(String content,Object... args) {
        for (int i = 0; i < Thread.currentThread().getStackTrace().length; i++) {
            String realContent = getContent(content, i, args);
            Log.d("waring", realContent);
        }
    }

    public static void debug(String msg) {
        Log.d("kdebug", msg);
    }
}